package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/8/8
 * Description:
 */

public class RatioLinearLayout extends LinearLayout {
    private float mRatio;
    public RatioLinearLayout(Context context) {
        super(context);
    }

    public RatioLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.RatioLinearLayout);

        mRatio=array.getFloat(R.styleable.RatioLinearLayout_ratio,1.0f);

        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec=MeasureSpec.makeMeasureSpec(
                (int) (MeasureSpec.getSize(widthMeasureSpec)*mRatio),
                MeasureSpec.getMode(widthMeasureSpec)
        );

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
